<html>
<head><meta charset="utf-8"><title>const fn closure · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20fn.20closure.html">const fn closure</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="202521892"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20fn%20closure/near/202521892" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jarod <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20fn.20closure.html#202521892">(Jul 01 2020 at 01:32)</a>:</h4>
<p>Would it be reasonable in the future for rust to be able to create const fn closures? Would they close over any variables if they have static lifetimes?</p>



<a name="202527789"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20fn%20closure/near/202527789" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20fn.20closure.html#202527789">(Jul 01 2020 at 03:36)</a>:</h4>
<p><code>'static</code> bounds are orthogonal from <code>const fn</code>-ness</p>



<a name="202527835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20fn%20closure/near/202527835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20fn.20closure.html#202527835">(Jul 01 2020 at 03:36)</a>:</h4>
<p>the missing thing is either a syntax, or an inference step, to determine whether the closure should be considered <code>const fn</code></p>



<a name="202527854"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20fn%20closure/near/202527854" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20fn.20closure.html#202527854">(Jul 01 2020 at 03:37)</a>:</h4>
<p>the closed over variables are just like fields in a <code>struct</code> that is passed to the closure</p>



<a name="202532903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20fn%20closure/near/202532903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20fn.20closure.html#202532903">(Jul 01 2020 at 05:40)</a>:</h4>
<p>I'll add them to the skill tree</p>



<a name="202533237"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20fn%20closure/near/202533237" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20fn.20closure.html#202533237">(Jul 01 2020 at 05:48)</a>:</h4>
<p>done. Basically we'll need the ability to invoke trait methods, too. We already have an impl for that if the closure isn't a generic parameter but defined in the method where it is called</p>



<a name="202533251"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20fn%20closure/near/202533251" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20fn.20closure.html#202533251">(Jul 01 2020 at 05:49)</a>:</h4>
<p>so yea, I guess all that is left is to auto-force the <code>Fn*</code> impls for closures in <code>const fn</code> to be <code>const impl</code>s</p>



<a name="202620963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20fn%20closure/near/202620963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20fn.20closure.html#202620963">(Jul 01 2020 at 20:36)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> are you sure though? I thought I've used closures in constant contexts before, by casting them to a <code>fn()</code> pointer or <code>&amp;dyn Fn()</code></p>



<a name="202654912"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20fn%20closure/near/202654912" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20fn.20closure.html#202654912">(Jul 02 2020 at 06:43)</a>:</h4>
<p>yea, but you can't do it in const fn, so we can do this inside const fn</p>



<a name="202824444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20fn%20closure/near/202824444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20fn.20closure.html#202824444">(Jul 03 2020 at 17:28)</a>:</h4>
<p>I'm not too happy about the idea tbh, it feels kind of arbitrary. unless you can put runtime code in the closure and it just wouldn't be considered <code>const Fn()</code> but idk which parts of the compiler check that aspect of <code>const Trait</code>-ness</p>



<a name="202880412"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20fn%20closure/near/202880412" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20fn.20closure.html#202880412">(Jul 04 2020 at 16:13)</a>:</h4>
<p>interesting idea. I think it could be a reasonable scheme to have closures infer their constness</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>